package rad.framework.conv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * These classes are for performing conversions from one database version to
 * another as the data model changes.
 * 
 * @author boudyacho
 * 
 */
public class Controller {

	protected static final Log log = LogFactory.getLog(Controller.class);
	
	protected List<BaseCommand> list = new ArrayList<BaseCommand>();

	protected Map<String, BaseCommand> map = new HashMap<String, BaseCommand>();

	protected String names = null;

	protected String path = null;

	public Controller(String path) {
		this.path = path;
	}

	public Controller(String path, String names) {
		this.path = path;
		this.names = names;
	}

	public void execute() throws Exception {
		log.info("execute started");
		init();
		if (names == null) {
			for (BaseCommand script : list) {
				script.execute();
			}
		} else {
			String[] tokens = names.split(",");
			for (String name : tokens) {
				map.get(name).execute();
			}
		}
		log.info("execute complete");
	}

	protected void init() {
	}

	protected void addEntity(String name, BaseCommand script) {
		list.add(script);
		map.put(name, script);
		script.setPath(path);
		// TODO persistence context?
	}
}
